<script setup>
import { ref, nextTick } from 'vue'
import { carPageQueryService } from '@/api/car.js'
import { Search, Refresh } from '@element-plus/icons-vue'

// 控制弹窗显示
const visibleDialog = ref(false)

const carList = ref([]) // 车辆列表
const total = ref(0) // 总条数
const loading = ref(false) // loading状态

// 表格选中项
const emit = defineEmits(['add']) // 定义自定义事件
const selectedCar = ref()
const tableRef = ref()

// 请求参数
const params = ref({
  pageNum: 1, // 当前页码
  pageSize: 10, // 每页条数
  orderBy: 'update_time', // 排序字段
  isAsc: false, // 是否升序
  memberId: null, // 会员id
  plateNumber: null, // 车牌号
  brand: null, // 车辆品牌
  model: null // 车辆型号
})

// 基于params参数，获取车辆列表
const getCarList = async () => {
  loading.value = true
  const res = await carPageQueryService(params.value)
  carList.value = res.data.data.list
  total.value = res.data.data.total
  loading.value = false
}
getCarList()

// 处理分页逻辑
const onPageSizeChange = (size) => {
  params.value.pageNum = 1
  params.value.pageSize = size
  getCarList()
}
const onPageNumChange = (page) => {
  params.value.pageNum = page
  getCarList()
}

// 显示会员信息
const activeMemberId = ref() // 当前需要显示的会员ID
const showMemberInfoForm = (memberId) => {
  activeMemberId.value = memberId
}

// 搜索逻辑
const handleSearch = () => {
  params.value.pageNum = 1
  getCarList()
}

// 重置逻辑
const handleReset = () => {
  params.value = {
    ...params.value, // 保留其他属性
    pageNum: 1,
    plateNumber: null,
    brand: null,
    model: null
  }
  getCarList()
}

// 处理选中项变化
const handleCurrentChange = (currentRow) => {
  selectedCar.value = currentRow
}

// 添加选中物品
const handleAddSelected = () => {
  emit('add', selectedCar.value) // 触发事件
  visibleDialog.value = false
}

// 向外暴露方法
const open = (memberId) => {
  params.value.memberId = memberId
  visibleDialog.value = true // 打开抽屉
  getCarList()
  // 清空选中状态
  nextTick(() => {
    tableRef.value?.setCurrentRow()
  })
}

defineExpose({
  open
})
</script>

<template>
  <el-dialog v-model="visibleDialog" width="60%">
    <page-container title="选择车辆">
      <template #extra>
        <el-button type="primary" plain @click="handleAddSelected" :disabled="!selectedCar">
          添加已选中车辆
        </el-button>
      </template>
      <!-- 表单 -->
      <el-form inline>
        <el-form-item label="车牌号:"
          ><el-input v-model="params.plateNumber" placeholder="请输入"
        /></el-form-item>
        <el-form-item label="车辆品牌:"
          ><el-input v-model="params.brand" placeholder="请输入"
        /></el-form-item>
        <el-form-item label="车辆型号:"
          ><el-input v-model="params.model" placeholder="1"
        /></el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch"></el-button>
          <el-button type="default" :icon="Refresh" @click="handleReset"></el-button>
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <el-table
        ref="tableRef"
        @current-change="handleCurrentChange"
        row-key="id"
        class="tb"
        :data="carList"
        v-loading="loading"
        highlight-current-row
        :row-style="{ height: '50px' }"
      >
        <el-table-column prop="id" label="车辆ID" width="150" />
        <el-table-column prop="memberId" label="会员ID" width="180">
          <template #default="{ row }">
            <el-popover effect="light" trigger="click" placement="right" :width="600">
              <template #reference>
                <el-tag @click="showMemberInfoForm(row.memberId)">{{ row.memberId }}</el-tag>
              </template>
              <MemberInfoForm v-if="activeMemberId === row.memberId" :memberId="row.memberId" />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="plateNumber" label="车牌号" width="180" />
        <el-table-column prop="brand" label="车辆品牌" width="180" />
        <el-table-column prop="model" label="车辆型号" width="180" />
        <el-table-column prop="lastServiceTime" label="上次服务时间" width="180" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column prop="updateTime" label="更新时间" width="180" />
        <!-- 空数据提示 -->
        <template #empty>
          <el-empty description="没有数据"></el-empty>
        </template>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        v-model:current-page="params.pageNum"
        v-model:page-size="params.pageSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="onPageSizeChange"
        @current-change="onPageNumChange"
        style="margin-top: 20px; justify-content: flex-end"
      />
    </page-container>
  </el-dialog>
</template>

<style lang="scss" scoped>
.tb {
  border-radius: 10px;
  box-shadow: 0 5px 10px 0 rgba(0, 0, 0, 0.05);
  overflow: hidden;
}
</style>
